var _hmt = _hmt || [];
(function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?4baf189c2dc5a9d55741220d381f4f47";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();

// 1.校验后台是否返回报错
function checkResult(result) {
    if (result.status != 0000) {
        layui.use(["layer"],function () {
            var layer = layui.layer;
            layer.msg(result.detail,{icon:2});
            console.log("code:" + result.status + ",detail:" + result.detail+",data:"+result.data);
        });
        return false;
    }
    return true;
}

//2.校验是否为空
function isNull(str){
    return null == str || "" == str || undefined == str;
}

//3.获取地址参数
function getUrlParms(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
//4.退出登录
function logOut(isLogOut){
    if(isLogOut){
        delCookie("managerId");
        delCookie("managerName");
        delCookie("username");
        delCookie("managerFacePic");
        delCookie("managerLevel");
        top.location.href = "/CivilAffairs/login/index.html";
    }
}

//5.校验登录状态
function isNotLogIn(){
    var managerId = getCookie("managerId");
    if(isNull(managerId)){
        layui.layer.msg("登录状态已失效，请先登录账号",{icon:2});
        setTimeout(function () {
            logOut(true);
        },500);
        return true;
    }
    return false;
}

/**
 * 6.请求异常
 * @param result
 */
function reqError(result,detail){
    console.log(detail+":"+result.status+","+result.statusMessage);
    layui.layer.msg(detail+",请稍后重试", { icon: 2 });
}

/**
 * 7.去除全选标记
 */
function removeAllSel(){
    layui.$("#selectAll").removeAttr("checked");
    layui.form.render("checkbox");
}

/**
 * cookie的一些js
 * @param name
 * @returns {*}
 */

//获得coolie 的值

function cookie(name){

    var cookieArray=document.cookie.split("; "); //得到分割的cookie名值对

    for (var i=0;i<cookieArray.length;i++){

        var arr=cookieArray[i].split("=");       //将名和值分开

        if(arr[0]==name)return unescape(arr[1]); //如果是指定的cookie，则返回它的值

    }

    return "";

}



/*function delCookie(name)//删除cookie

{

   document.cookie = name+"=;expires="+(new Date(0)).toGMTString();

}*/



function getCookie(objName){//获取指定名称的cookie的值

    var arrStr = document.cookie.split("; ");

    for(var i = 0;i < arrStr.length;i ++){

        var temp = arrStr[i].split("=");

        if(temp[0] == objName) return unescape(temp[1]);

    }

}



function addCookie(objName,objValue,objHours){      //添加cookie

    var str = objName + "=" + escape(objValue);

    if(objHours > 0){ //为时不设定过期时间，浏览器关闭时cookie自动消失

        var date = new Date();

        var ms = objHours*3600*1000;

        date.setTime(date.getTime() + ms);

        str += "; expires=" + date.toGMTString()+"; path=/";

    }

    document.cookie = str+"; path=/";

}



function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值

{

    var Days = 30; //此 cookie 将被保存 30 天

    var exp = new Date();    //new Date("December 31, 9998");

    exp.setTime(exp.getTime() + Days*24*60*60*1000);

    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

}

function getCookie(name)//取cookies函数

{

    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));

    if(arr != null) return unescape(arr[2]); return null;



}

function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) {
        document.cookie= name + "="+cval+";expires="+exp.toGMTString()+"; path=/";;
    }
}

